// pages/news/list.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    news_type: 0,
    page: 1,
    dataList: [],
    // 显示加载更多 loading
    hothidden: true,
    loadinghidden: true,
    isTouchstart: false,
    isLongtap: false,
    isTouchmove: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    Object.assign(this, ImgListCell);

    var type = options.type;
    if (!type) {
      wx.showModal({
        title: '加载失败！',
        success: function(res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    } else {

      var title = "";
      if (type == 4) {
        title = "我的草稿";
      } else if (type == 5) {
        title = "回收站";
      }

      wx.setNavigationBarTitle({
        title: title,
      });

      that.setData({
        news_type: type
      });
    }


    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    /**
     * 显示 loading
     */
    that.setData({
      loadinghidden: false
    });
    that.doLoadData();
  },
  onReady: function() {
    // 生命周期函数--监听页面初次渲染完成
    // 页面渲染完成
    var that = this;

    // 数据加载完成后 延迟隐藏loading
    setTimeout(function() {
      that.setData({
        loadinghidden: true
      })
    }, 500);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  doLoadData: function() {
    var that = this;

    var parameter = {};
    parameter['news_type'] = that.data.news_type;
    parameter['column_id'] = 0;
    parameter['keyword'] = "";
    parameter['page'] = 1;

    http.ApiGet(apiUrl.UrlNewsList, parameter, function(res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.code == 1) {
        var newData = res.data.list;
        if (newData.length > 0) {
          newData = newData.map(function(item) {
            item.autor = item.admin_nickname;
            if (item.images.length > 0) {
              item.thumb = item.images;
            }
            if (item.check_status == 99) {
              item.label = "已发布";
            } else if (item.check_status == 1) {
              item.label = "一审";
            } else if (item.check_status == 2) {
              item.label = "二审";
            } else if (item.check_status == 3) {
              item.label = "三审";
            } else if (item.check_status == 4) {
              item.label = "四审";
            } else if (item.check_status == -99) {
              item.label = "草稿";
            } else {
              item.label = "退稿";
            }

            if (that.data.news_type == 5) {
              item.label = "已删除";
            }

            if (item.video_url.length > 0) {
              item.has_video = 1;
            }

            return item;
          })
        } else {
          newData = [];
          // wx.showModal({
          //   title: "暂无数据！"
          // });
        }

        that.setData({
          page: 1,
          hothidden: false,
          dataList: newData
        });
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.doLoadData();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;
    // 获取当前现有数据进行保存
    var list = that.data.dataList;
    var parameter = {};
    parameter['news_type'] = that.data.news_type;
    parameter['column_id'] = 0;
    parameter['keyword'] = "";
    parameter['page'] = page;

    http.ApiGet(apiUrl.UrlNewsList, parameter, function(res) {

      if (res.code == 1) {
        var newData = res.data.list;
        if (newData.length > 0) {

          that.setData({
            page: page,
            hothidden: false,
            dataList: list.concat(newData.map(function(item) {
              item.autor = item.admin_nickname;
              if (item.images.length > 0) {
                item.thumb = item.images;
              }
              if (item.check_status == 99) {
                item.label = "已发布";
              } else if (item.check_status == 1) {
                item.label = "一审";
              } else if (item.check_status == 2) {
                item.label = "二审";
              } else if (item.check_status == 3) {
                item.label = "三审";
              } else if (item.check_status == 4) {
                item.label = "四审";
              } else if (item.check_status == -99) {
                item.label = "草稿";
              } else {
                item.label = "退稿";
              }

              if (that.data.news_type == 5) {
                item.label = "已删除";
              }

              if (item.video_url.length > 0) {
                item.has_video = 1;
              }

              return item;
            }))
          });
        } else {
          that.setData({
            hothidden: false
          })
        }
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
  //按下事件开始  
  doTouchstart: function(e) {
    let that = this;

    var index = e.currentTarget.dataset.index;

    // var title = that.data.dataList[index].title;
    var item = that.data.dataList[index];
    var news_id = item.id;
    var check_status = item.check_status;
    console.log(news_id);

    that.setData({
      isTouchstart: true,
      isLongtap: false
    })

    setTimeout(function() {
      if (that.data.isTouchstart && !that.data.isTouchmove) {
        that.setData({
          isLongtap: true
        })

        // console.log("到时间了,是长按");
        var itemList = [];
        if (check_status == 99) {

          if (that.data.news_type == 3) {
            itemList = ['设置置顶', '设置头图'];
          }
          itemList.push('取消发布');

        } else {
          itemList = ['修改', '审核'];

          var user = wx.getStorageSync("user");
          var admin_id = item.admin_id;
          if (user) {
            if (admin_id != user.id) {
              itemList.push('退稿');
            } else {
              if (that.data.news_type != 5) {
                itemList.push('删除');
              }
            }
          }
        }


        wx.showActionSheet({
          itemList: itemList,
          success: function(res) {
            if (!res.cancel) {
              var text = itemList[res.tapIndex];
              console.log(text);
              if (text == "删除") {
                that.doDelete(news_id);
              } else if (text == "修改") {
                if ((item.show_type == 1 && item.skip_type == 2) || (item.show_type == 1 && item.skip_type == 3) || item.show_type == 4 || item.show_type == 5) {
                  wx.showModal({
                    title: "专题、外链暂不支持操作！"
                  });
                } else {
                  if (item.show_type == 2) {
                    wx.navigateTo({
                      url: '../news/imgSubmit?id=' + news_id
                    });
                  } else {
                    wx.navigateTo({
                      url: '../news/submit?id=' + news_id
                    });
                  }
                }
              } else if (text == "审核") {
                that.doPost(apiUrl.UrlNewsCheck, {
                  "news_id": news_id
                });
              } else if (text == "退稿") {
                wx.showModal({
                  title: "暂不支持的操作！"
                });
              } else if (text == "设置置顶") {
                var is_top = item.is_top;
                if (is_top == 1) {
                  is_top = 0;
                } else {
                  is_top = 1;
                }
                that.doPost(apiUrl.UrlNewsSetting, {
                  "news_id": news_id,
                  "is_top": is_top
                });
              } else if (text == "设置头图") {
                var is_head = item.is_head;
                if (is_head == 1) {
                  is_head = 0;
                } else {
                  is_head = 1;
                }
                that.doPost(apiUrl.UrlNewsSetting, {
                  "news_id": news_id,
                  "is_head": is_head
                });
              } else if (text == "取消发布") {
                that.doPost(apiUrl.UrlNewsCancel, {
                  "news_id": news_id
                });
              } else {
                wx.showModal({
                  title: "暂不支持的操作！"
                });
              }
            }
          }
        })

      }
    }, 500);
  },
  //按下事件结束  
  doTouchend: function(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var item = that.data.dataList[index];
    var id = item.id;

    that.setData({
      isTouchstart: false
    })

    if (!that.data.isLongtap && !that.data.isTouchmove) {
      console.log("点击");

      var user = wx.getStorageSync("user");
      var appid = 0;
      if (user) {
        appid = user.appid;
      }

      var link = "";

      //外链
      if ((item.show_type == 1 && item.skip_type == 3) || item.show_type == 5) {
        link = item.skip_data
      }
      //专题
      if ((item.show_type == 1 && item.skip_type == 2) || item.show_type == 4) {
        console.log(item.skip_special_id);
        link = "https://www.sogx.cn/wap/special/list/appid/" + appid + "/id/" + item.skip_special_id + "/is_app/1"
      }

      //图集
      if (item.show_type == 2) {
        link = "https://www.sogx.cn/wap/news/detail/appid/" + appid + "/id/" + item.id + "/is_app/1"
      }

      console.log(link);

      if (link.length > 0) {
        // 检查链接地址符合下面的才能打开
        // "https://laigao.longsunhd.com"
        // "https://v.sogx.cn"
        // "https://www.sogx.cn"

        if (link.indexOf("v.sogx.cn") > 0 || link.indexOf("laigao.longsunhd.com") > 0 || link.indexOf("www.sogx.cn") > 0) {
          link = link.replace("http:", "https:");
          link = link.replace(":8080", "");
          wx.navigateTo({
            url: '../webview/index?url=' + link
          })
        } else {
          wx.showModal({
            title: "暂不支持打开此链接",
            cancelText: '取消',
            confirmText: '复制',
            success(res) {
              if (res.confirm) {
                wx.setClipboardData({
                  data: link,
                  success: function(res) {
                    wx.showToast({
                      title: "复制成功！",
                      icon: 'success',
                      duration: 1000
                    });
                  }
                });
              }
            }
          });
          return;
        }

      } else {

        wx.navigateTo({
          url: '../news/detail?id=' + item.id
        })
      }
    }
    that.setData({
      isTouchmove: false
    });

  },
  doTouchmove: function(e) {
    let that = this;
    that.setData({
      isTouchmove: true
    })
    console.log("滑动");
  },
  doDelete: function(id) {
    let that = this;

    var formData = {};
    formData['news_id'] = id;

    wx.showModal({
      title: '确定删除？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          http.ApiPost(apiUrl.UrlNewsDelete, formData, function(res) {
            if (res.code == 1) {
              that.doLoadData();
            } else {
              wx.showModal({
                title: res.msg
              });
            }
          });
        }
      }
    });
  },
  doPost: function(url, formData) {
    let that = this;
    http.ApiPost(url, formData, function(res) {
      if (res.code == 1) {
        that.doLoadData();
      } else {
        wx.showModal({
          title: res.msg
        });
      }
    });
  },
})